﻿using Helpers;
using System;
using System.IO.Ports;
using System.Threading;
using System.Windows;

namespace GreenXi.Help
{

    /// <summary>
    /// 对接扫码枪类
    /// </summary>
    public class RQCodeHelp
    {
        /// <summary>
        /// 初始化
        /// </summary>
        public static void Init()
        {
            try
            {
                GlobalVariables.RQServoMotor = new SerialPort(GlobalVariables.OP10PortName, int.Parse(GlobalVariables.OP10BaudRate), Parity.None, 8, StopBits.One);
                if (!GlobalVariables.RQServoMotor.IsOpen)
                {
                    GlobalVariables.RQServoMotor.Open();
                }
                GlobalVariables.RQServoMotor.DataReceived += serialscanner_dealwithbarcode;
            }
            catch (Exception ex)
            {
                Logger.Instance.Error(ex, "扫码枪连接失败");
                MessageBox.Show("扫码枪连接失败");
            }
        }
        /// <summary>
        /// 扫码完成事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private static void serialscanner_dealwithbarcode(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            Thread.Sleep(100);
            string BarCode = sp.ReadExisting().Replace("\r\n", String.Empty);
            if (BarCode == string.Empty)
            {
                return;
            }
            GlobalVariables.BarcodeInfo1 = BarCode;

        }

        public static string CreatCode128(string codeVw, string codeHz, string offset_UL, string offset_UT, string offset_DL, string offset_DT)
        {
            string str = "^XA"
                        + "^MMT" //打印模式 T撕扯
                        + "^PW1200" //打印宽度
                        + "^LL1600" //标签长度
                        + "^LS0" //标签移位 
                        + "^LH0,0"//设置标签起始位置
                        + "^PR1"//设置打印速度 1最慢
                        + "^FO100,100"//设置字段起始位置
                                      //第一行二维码
                        + "^FT" + offset_UL + "," + offset_UT //字段排版x轴位置，y轴位置，对齐方式（0左，1右，2自动）
                                                              //+ "^BXI,6,200,24,24"//
                        + "^BY3,3.0,150"

                        + "^BCN,,N"//,150,Y"//设置Code128一维码格式
                        + "^FH^FD" + codeHz + "^FS" //FH16进制 以_为开头
                        + "^FT" + (Convert.ToInt32(offset_UL) + 150).ToString() + "," + (Convert.ToInt32(offset_UT) + 50).ToString()
                        + "^A0N,46,45"
                        + "^FB400,1,0,C"
                        + "^FH^FD" + codeHz + "^FS"
                        //第二行二维码
                        + "^FT" + offset_DL + "," + offset_DT
                        + "^BY3,3.0,150"
                        + "^BCN,,N"
                        + "^FH^FD" + codeVw + "^FS"
                        + "^FT" + (Convert.ToInt32(offset_DL) + 150).ToString() + "," + (Convert.ToInt32(offset_DT) + 50).ToString()
                        + "^A0N,46,45"
                        + "^FB400,1,0,C"
                        + "^FH^FD" + codeVw + "^FS"
                        + "^XZ";

            return str;
        }

        /// <summary>
        /// ZPL指令打印条码
        /// </summary>
        /// <param name="barcode"></param>
        /// <returns></returns>
        public static string CreateBarcode(string tempBarcode)
        {
            string barcode = "^XA"
                 + "^LH" + 0 + "," + 0 + ""
                  + "^LL1600" //标签长度
                  + "^FT300,30"
                 + "^A0N,33,35"
                 + "^FD" + "E22H" + "^FS"
                 + "^FT24,130^BY2"
                 + "^BCN,90,N,N"
                 + "^FD" + tempBarcode + "^FS"
                 + "^FT145,160"
                 + "^A0N,33,35"
                 + "^FD" + tempBarcode + "^FS"
                 + "^XZ";

            return barcode;

        }
        /// <summary>
        /// 打印二维码
        /// </summary>
        /// <param name="tempBarcode"></param>
        /// <returns></returns>
        public static string CreateBarcode2(string tempBarcode)
        {
            var num = 7;
            if (tempBarcode.Length > 27)
            {
                num = 8;
            }
            string barcode = "^XA"
               //二维码
               + "^FO100,20"
               + "^BXN,7,200"
               + "^FH"
               + "^FD" + tempBarcode + "^FS"
               //第一行
               + "^FO280,20"
               + "^A0N,33,36"
               + "^FD" + tempBarcode.Substring(0, 11) + "^FS"
                //第二行
                + "^FO280,70"
               + "^A0N,33,36"
               + "^FD" + tempBarcode.Substring(11, 9) + "^FS"
                //第三行
                + "^FO280,120"
               + "^A0N,33,36"
               + "^FD" + tempBarcode.Substring(20, num) + "^FS"
               + "^XZ";
            return barcode;
        }
    }
}
